package com.haochu.novel.ui;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testnovel.R;
import com.haochu.novel.util.Constants;
import com.haochu.novel.vo.Category;

/**
 * @author fuhuichao
 * 2017年6月20日17:14:02
 * 小说目录
 */
public class BookCategoryActivity extends Activity implements OnScrollListener, OnItemClickListener {

	private mAdapter adapter;
	private ListView mBookCateGoryView;
	// listview的数据集合
	private List<Category> catogories;
	// 返回阅读页时传递的参数
	private Bundle returnBundle;
	// 标记当前页的章节目录下标
	private Category currentCategory;
	// 标记断章是否完成
	private boolean categoryFinish;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//设置无标题  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        //设置全屏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		
		setContentView(R.layout.layout_book_category);
		returnBundle = new Bundle();
		
		// 绑定listview
		mBookCateGoryView = (ListView) findViewById(R.id.mBookCateGoryView);
		
		// 绑定目录列表数据
		initData();
		adapter = new mAdapter(this, currentCategory);
		mBookCateGoryView.setAdapter(adapter);
		// 滚动到当前阅读的章节目录位置
		mBookCateGoryView.setSelection(currentCategory.getIndex());
		
		// 设置点击单项监听
		mBookCateGoryView.setOnItemClickListener(this);
		mBookCateGoryView.setOnScrollListener(this);
	}
	
	/**
	 * 初始化目录
	 */
	public void initData(){
		catogories = BookFactory.getToShowCategories();
		currentCategory = BookFactory.getCurCategory();
		categoryFinish = BookFactory.isCategoryFinish();
	}
	
	/**
	 * 点击单项监听
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		returnBundle.putInt(Constants.RESULT_KEY_CATEGORY, position);
		setResult(Constants.RESULT_CODE_CATEGORY, new Intent().putExtras(returnBundle));
		finish();
	}

	/**
	 * 监听滚动，若滚动条到底部且目录还未加载完，则重新加载
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if(!categoryFinish && firstVisibleItem + visibleItemCount >= totalItemCount){
			initData();
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {}
	
	/**
	 * 重写自定义的adapter
	 */
	class mAdapter extends BaseAdapter{

		private LayoutInflater mInflater;
		private Category currentCategory;
		
		public mAdapter(Context context, Category currentCategory){
			this.mInflater = LayoutInflater.from(BookCategoryActivity.this);
			this.currentCategory = currentCategory;
		}
		
		@Override
		public int getCount() {
			return catogories.size();
		}

		@Override
		public Object getItem(int position) {
			return catogories.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams") 
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
			}
			TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
			textView.setText(catogories.get(position).getName());
			
			// 设置选中的行状态
			if(this.currentCategory.getId() == catogories.get(position).getId()){
				textView.setTextColor(Color.rgb(250, 90, 0));
			}else{
				textView.setTextColor(Color.BLACK);
			}
			
			return convertView;
		}
		
		
	}
}

